package com.ly.drivingschool.controller;

import com.alibaba.fastjson.JSONObject;
import com.ly.drivingschool.common.ResponseMessage;
import com.ly.drivingschool.config.WechatConfig;
import com.ly.drivingschool.dao.CountDao;
import com.ly.drivingschool.dao.StaffDao;
import com.ly.drivingschool.dao.UserDao;
import com.ly.drivingschool.pojo.Info;
import com.ly.drivingschool.pojo.Staff;
import com.ly.drivingschool.pojo.User;
import com.ly.drivingschool.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信小程序端 请求
 */
@Slf4j
@RestController
@RequestMapping(value = "wxApi")
public class WxController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private StaffDao staffDao;

    @Autowired
    private CountDao countDao;

    /**
     * 微信登录
     * 获取openId
     * @return
     */
    @GetMapping("wxLogin")
    public ResponseMessage wxLogin(String code){
        // 配置请求参数
        Map<String, String> param = new HashMap<>();
        param.put("appid", WechatConfig.APP_ID);
        param.put("secret", WechatConfig.APP_SECRET);
        param.put("js_code", code);
        param.put("grant_type", WechatConfig.WX_LOGIN_GRANT_TYPE);

        // 向微信服务端发送请求
        // 发送请求
        String wxResult = HttpClientUtil.doGet(WechatConfig.WX_LOGIN_URL, param);
        JSONObject jsonObject = JSONObject.parseObject(wxResult);
        System.out.println(jsonObject.toString());
        if (jsonObject.containsKey("openid")) {
            //存在openid值，将返回的openid，sessionKey存入map
            Map<String, Object> map = new HashMap<>();
            // 获取参数返回的
            String session_key = jsonObject.get("session_key").toString();
            String open_id = jsonObject.get("openid").toString();
            map.put("openid", open_id);
            map.put("session_key", session_key);
            // 根据openId校验是否存在学员/教练/没有
            User user = userDao.selectByOpenId(open_id);
            Staff staff = staffDao.selectByOpenId(open_id);

            if (user != null) {
                // 是学员 isLoginState==0
                map.put("isLoginState",0);
                map.put("info",user);
                return ResponseMessage.createBySuccess(map);
            } else if (staff != null) {
                // 是教练
                map.put("isLoginState",1);
                map.put("info",staff);
                // 返回信息
                return ResponseMessage.createBySuccess(map);
            } else {
                // 都不是 是游客
                map.put("isLoginState",2);
                map.put("info",null);
                // 返回信息
                return ResponseMessage.createBySuccess(map);
            }
        } else {
            return ResponseMessage.createByErrorMessage(jsonObject.get("errmsg").toString());
        }
    }

    /**
     * 获取用户信息
     * @param open_id
     * @return
     */
    @GetMapping("getUserInfo")
    public ResponseMessage checkLogin(String open_id) {
        System.out.println(open_id);
        Info info = countDao.getInfo(open_id);
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("info",info);
        return ResponseMessage.createBySuccess("个人信息",hashMap);
    }

    /**
     * 绑定个人
     * 根据身份证号码 绑定微信openId
     * idCard wxOpenId
     * @param idCard
     * @param radio 1 表示学员 2表示教师
     * @return
     */
    @GetMapping("bindInfo")
    public ResponseMessage bindInfo(String idCard,String open_id,int radio){
        System.out.println(radio);
         // 判断是学员还是教练
         if (radio == 1) {
             // 你是学员 绑定学员
             // 封装数据
             User user = new User();
             user.setIdCard(idCard);
             user.setWxOpenId(open_id);
             int code = userDao.updateByIdCard(user);
             System.out.println(code);
             return ResponseMessage.createBySuccess("学员绑定成功");
         } else if (radio == 2) {
             Staff staff = new Staff();
             staff.setIdCard(idCard);
             staff.setWxOpenId(open_id);
             int code = staffDao.updateByIdCard(staff);
             return ResponseMessage.createBySuccess("教练绑定成功");
         }else {
             return ResponseMessage.createByErrorMessage("出现错误，请联系管理员");
         }
    }

    /**
     * 根据open_id 获取教练所带学员信息
     * @param open_id
     * @return
     */
    @GetMapping("getUserListByStaff")
    private ResponseMessage getUserListByStaff(String open_id) {
        List<User> userList = countDao.getUserListByStaff(open_id);
        return ResponseMessage.createBySuccess("我的学员",userList);
    }
}
